package com.waimai.app.qa.webdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.waimai.app.qa.util.Logger;

import io.appium.java_client.android.AndroidDriver;

public class SearchOperate {
	public static AndroidDriver<WebElement> driver = (AndroidDriver<WebElement>) AppDriver.driver;

	/**
	 * @Description：循环在页面中查找文案 并点击
	 * @author：hyzhao 
	 * @time：2017年7月13日 下午4:33:33
	 */
	public static void WaitForStringShowAndClick_Scroll(String strings) {
		WebDriverWait wait = new WebDriverWait(driver, 10, 2000);
		String strBefore = "new UiSelector().text(\"";
		String strBehind = "\")";

		for (int i = 1; i <= 30; i++) {
			Logger.log("第" + i + "次查找：" + strBefore + strings + strBehind);
			try {
				wait.until(new ExpectedCondition<WebElement>() {
					@Override
					public WebElement apply(WebDriver driver1) {
						// TODO Auto-generated method stub
						return ((AndroidDriver<WebElement>) driver1)
								.findElementByAndroidUIAutomator(strBefore + strings + strBehind);
					}
				}).click();
				break;
			} catch (Exception e) {
				AppDriver.swipeToUp(4000);
				continue;
			}
		}
	}
	
	
	/**
	 * @Description：循环在页面中查找文案 
	 * @author：hyzhao 
	 * @time：2017年7月13日 下午4:33:33
	 */
	public static void WaitForStringShowAndShow_Scroll(String strings) {
		WebDriverWait wait = new WebDriverWait(driver, 10, 2000);
		String strBefore = "new UiSelector().text(\"";
		String strBehind = "\")";

		for (int i = 1; i <= 30; i++) {
			Logger.log("第" + i + "次查找：" + strBefore + strings + strBehind);
			try {
				wait.until(new ExpectedCondition<WebElement>() {
					@Override
					public WebElement apply(WebDriver driver1) {
						// TODO Auto-generated method stub
						return ((AndroidDriver<WebElement>) driver1)
								.findElementByAndroidUIAutomator(strBefore + strings + strBehind);
					}
				}).isDisplayed();
				break;
			} catch (Exception e) {
				AppDriver.swipeToUp(500);
				continue;
			}
		}
	}
}
